package com.tianyang.yukuaifu.business.login.view;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.YkfApplication;
import com.tianyang.yukuaifu.base.PermissionActivity;
import com.tianyang.yukuaifu.business.login.bean.LoginOutput;
import com.tianyang.yukuaifu.business.main.view.MainActivity;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.dialog.PrivacyDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IGlobalListener;
import com.tianyang.yukuaifu.utils.AppContextUtil;
import com.tianyang.yukuaifu.utils.IntentTool;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Description：APP闪屏页面
 */
public class SplashActivity extends PermissionActivity {

    private int loadTime = 1000;

    private Timer timer;

    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        Window window = getWindow();
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);

        setContentView(R.layout.activity_splash);

        //全屏显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 1001) {
                    showPrivacyDialog();
                }
                return false;
            }
        });

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                boolean firstLaunch = CacheManage.getInstance().getCache(CacheModel.FIRST_REMINDER, true);
                if (firstLaunch) {
                    Message msg = Message.obtain();
                    msg.what = 1001;
                    mHandler.sendMessage(msg);
                } else {
                    enterMain();
                }
            }
        }, loadTime);
    }

    private synchronized void showPrivacyDialog() {
        PrivacyDialog vPrivacyDialog = new PrivacyDialog(this);
        vPrivacyDialog.setGlobalListener(new IGlobalListener() {
            @Override
            public void onCancelClick() {
                IntentTool.finishActivity(SplashActivity.this);
            }

            @Override
            public void onConfirmClick() {
                CacheManage.getInstance().saveCache(CacheModel.FIRST_REMINDER, false);
                enterMain();
            }
        });
        vPrivacyDialog.show();
    }

    private synchronized void enterMain() {
        YkfApplication.getInstance().initModule();
        try {
            LoginOutput loginOutput = CacheManage.getInstance().getCache(CacheModel.LOGIN_KEY);
            String extras = getIntent().getStringExtra(CommonConfig.INTENT_EXTRAS);
            Intent intent = new Intent();
            if (loginOutput != null) {
                intent.setClass(this, MainActivity.class);
                intent.putExtra(CommonConfig.INTENT_ISTRANSPUSH,
                        getIntent().getBooleanExtra(CommonConfig.INTENT_ISTRANSPUSH, false));
            } else {
                intent.setClass(this, LoginActivity.class);
            }
            if (!TextUtils.isEmpty(extras)) {
                intent.putExtra(CommonConfig.INTENT_EXTRAS, extras);
            }
//            intent.setClass(this, MainActivity.class);
            IntentTool.startActivity(this, intent, true);
        } catch (ClassCastException e) {
            CacheManage.getInstance().clear();
            AppContextUtil.finishAllExclByActivity(SplashActivity.class.getName());
            IntentTool.startActivity(this, LoginActivity.class, true);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}
